﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using BitPortal.Utils;

namespace BitPortal.Business
{
	public static class ErrorReportBLL
	{

        /* ******************************* CREAT-DELETE-UPDATE ************************************** */

		public static int Insert(this BitPortal.Models.ErrorReport errorReport)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			extBitPortal.ErrorReports.AddObject(errorReport);
			return extBitPortal.SaveChanges();
		}

        public static int DeletePermanently(long errorID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            ErrorReport error = bitEnt.ErrorReports.SingleOrDefault(a => a.ErrorID == errorID);
            if (error != null)
            {
                bitEnt.ErrorReports.DeleteObject(error);
                return bitEnt.SaveChanges();
            }
            return 0;

        }

		public static int Update( BitPortal.Models.ErrorReport errorReport)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			BitPortal.Models.ErrorReport error = extBitPortal.ErrorReports.SingleOrDefault(pp => pp.ErrorID == errorReport.ErrorID);
			error = errorReport;
			return extBitPortal.SaveChanges();
		}

        /* ************************************** GET ERRORs ***************************************** */

        /// <summary>
        /// Get all errors contains errorrs was seted isdeleted=true
        /// </summary>
        /// <returns></returns>
		public static IQueryable<BitPortal.Models.ErrorReport> GetAll()
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			IQueryable<BitPortal.Models.ErrorReport> list = from pp in extBitPortal.ErrorReports                                                          
                                                            orderby pp.ErrorID descending ,pp.DateReport descending
														    select pp;
			return list;								
		}

        /// <summary>
        /// get all errros of a customer
        /// </summary>
        /// <param name="customerID"> id of customer</param>
        /// <returns></returns>
		public static IQueryable<BitPortal.Models.ErrorReport> GetByCustomer(Int64 customerID)
		{
			ExtendedBitPortalEntities extBitPortal  =   new ExtendedBitPortalEntities();
			IQueryable<BitPortal.Models.ErrorReport> list   =   from pp in extBitPortal.ErrorReports
														        from cc in extBitPortal.ProductCustomers
														        from dd in extBitPortal.Customers
														        where pp.ProductCustomerId == cc.ProductCustomerId &&
																      cc.CustomerID == dd.CustomerID &&
																      dd.CustomerID == customerID
														        select pp;
			return list;
		}

        /// <summary>
        /// get all errors of an product
        /// </summary>
        /// <param name="productID"> id of product</param>
        /// <returns></returns>
		public static IQueryable<BitPortal.Models.ErrorReport> GetByProductID(Int64 productID)
		{
			ExtendedBitPortalEntities extBitPortal  =   new ExtendedBitPortalEntities();
			IQueryable<BitPortal.Models.ErrorReport> list = from pp in extBitPortal.ErrorReports
														    from cc in extBitPortal.ProductCustomers
														    where pp.ProductCustomerId == cc.ProductCustomerId &&
															  cc.ProductID == productID
														    select pp;
			return list;
														
		}

        /// <summary>
        /// get one errror by errrorID
        /// </summary>
        /// <param name="errorID"></param>
        /// <returns></returns>
		public static ErrorReport GetByErrorID(Int64 errorID)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			return extBitPortal.ErrorReports.SingleOrDefault(pp => pp.ErrorID == errorID);
		}

        /// <summary>
        /// Get all errors of an product of an customer
        /// </summary>
        /// <param name="productCustomerId"></param>
        /// <returns></returns>
        public static IQueryable<ErrorReport> GetByProductCustomer(long productCustomerId)
        {
            ExtendedBitPortalEntities extBitportal = new ExtendedBitPortalEntities();
            IQueryable<ErrorReport> list= from pp in extBitportal.ErrorReports
                                          where pp.ProductCustomerId == productCustomerId
                                          select pp;
            return list;
        }



        /// <summary>
        /// Get all errors of an product of an customers
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static IQueryable<ErrorReport> GetAllErrors(long errorId)
        {
            ExtendedBitPortalEntities ent = new ExtendedBitPortalEntities();
            ErrorReport error = ent.ErrorReports.SingleOrDefault(pp => pp.ErrorID == errorId);

            ProductCustomer productCustomer = ent.ProductCustomers.SingleOrDefault(pp => pp.ProductCustomerId == error.ProductCustomerId);
            IQueryable<ErrorReport> list = from pp in ent.ErrorReports
                                           where pp.ProductCustomerId==productCustomer.ProductCustomerId
                                           orderby pp.ErrorID descending
                                           select pp;
            return list;

        }
    }
}